<?php
namespace app\facility\controller;

use think\Controller;
use think\Cache;
use think\Db;

class Index extends Controller
{
    public function index()
    {
        $data = "4407799&bc:6c:21:10:9b:55|83;ac:18:26:e7:ad:2c|84;b4:ef:fb:0d:49:b2|91;18:dc:56:b9:2a:c4|91;48:db:50:72:b8:9e|104;ac:e0:10:20:d3:ed|125;3c:f5:91:ab:e8:7f|132;&85.00&140&2";
        //shebeiID .MAC TEMP ,miaoshu,dasongcishu
        str_replace("'", "\'", $data);
        $temp_array = explode("&", $data);
        $len = sizeof($temp_array);
        print_r($temp_array);
        if ($len == 5) {
            $mac_data = $temp_array[1];
            $sql = "insert into mac_gprs_tcp_tb1(device_id,content,temp,ticker_count,upload_count,add_time) values('"
                . $temp_array[0] . "','" . $mac_data . "','" . $temp_array[2] . "','"
                . $temp_array[3] . "','" . $temp_array[4] . "','" . date('Y-m-d H:i:s') . "')";
            DB::execute($sql);
            $temp_array2 = explode(";", $mac_data);
            print_r($temp_array2);
            $now_time_num = $temp_array[3];
            $curtime = time();
            foreach ($temp_array2 as $item) {
                if ($item != "") {
                    $temp_array3 = explode("|", $item);
                    if (sizeof($temp_array3) == 2) {
                        //echo $now_time_num . "\n";
                        //echo $temp_array3[1] . "\n";
                        $t1 = $now_time_num - $temp_array3[1];
                        $t2 = $curtime - $t1;
                        $sql2 = "insert into mac_gprs_tcp_tb2(mac,add_time,collect_time,device_id) values('" .
                            $temp_array3[0] . "','"
                            . date('Y-m-d H:i:s') . "','"
                            . date("Y-m-d H:i:s", $t2) . "','" . $temp_array[0] . "');";
                        Db::execute($sql2);
                    }
                }
            }
        }
    }

    // datasky路由+wifi探针
    public function datasky($data = '')
    {
    	// $data = '{"id":"","mmac":"5e:cf:7f:29:c5:91","rate":"1","time":"Tue Feb 21 08:13:31 2017","lat":"30.747988","lon":"103.973152" ,"data":[{"mac":"a4:56:02:61:7f:57","rssi":"-91","range":"91.5"},{"mac":"8c:a6:df:62:2d:3d","rssi":"-93","range":"108.5"},{"mac":"a4:56:02:71:be:b3","rssi":"-96","range":"140.1"},{"mac":"cc:34:29:97:4d:0d","rssi":"-95","range":"128.6"},{"mac":"44:33:4c:aa:71:82","rssi":"-94","range":"118.1"},{"mac":"b0:48:7a:5a:10:f8","rssi":"-86","range":"59.7"},{"mac":"a8:57:4e:9d:ca:d8","rssi":"-96","range":"140.1"},{"mac":"5e:cf:7f:93:3d:0e","rssi":"-56","range":"4.6"},{"mac":"5e:cf:7f:93:3d:0f","rssi":"-58","range":"5.5"},{"mac":"5e:cf:7f:93:3d:10","rssi":"-63","range":"8.4"},{"mac":"5e:cf:7f:93:3d:0b","rssi":"-68","range":"12.9"},{"mac":"5e:cf:7f:93:3d:0c","rssi":"-53","range":"3.5"},{"mac":"5e:cf:7f:93:3d:0d","rssi":"-69","range":"14.0"},{"mac":"e4:f3:f5:24:2c:d8","rssi":"-89","range":"77.1"},{"mac":"14:cf:92:8a:8f:f0","rssi":"-96","range":"140.1"}]}';

        $data = json_decode($data, true);
        if (!isset($data['id'])) {
            write_log("错误的mac信息" . json_encode($data), 2);
            return;
        }
        // dump($data);die;
        $saveData['collect_time'] = date('Y-m-d H:i:s', strtotime($data['time']));
        if (sizeof($data['data']) > 1) {
        	foreach ($data['data'] as $k => $val) {
        		if ($val != "") {
        			$saveData['mac'] = $val['mac'];
        			$saveData['add_time'] = date('Y-m-d H:i:s');
        			Db::table('z_datesky')->insert($saveData);
        		}
        	}
        }
        return 'ok';
    }

    public function bm($data = '')
    {
        // 'data' => '60:01:94:5d:54:3b|2017-11-15 11:54:25;00:0c:e7:74:82:74|2017-11-15 11:54:29;54:14:73:c3:c7:59|2017-11-15 11:54:33;38:29:5a:c2:8b:2f|2017-11-15 11:54:33;48:e2:44:58:87:23|2017-11-15 11:54:33;1c:77:f6:0a:8a:12|2017-11-15 11:54:33;02:36:4e:03:88:77|2017-11-15 11:54:34;',
        // 'id' => '4406709',
        // 'temp' => '26.44',
        // write_log(json_encode($data), 4);die;
        // $data = json_decode($data, true);
        // if (!isset($data['id'])) {
        //     write_log("博媒设备错误的mac信息" . json_encode($data), 4);
        //     return;
        // }
        // write_log($data, 4);
        $str = $data;
        $data1 = explode(';', $str);
        if (!$data1) {
            return;
        }

        foreach ($data1 as $key => $value) {
            if (!$value) {
                continue;
            }
            $arr = explode('|', $value);
            $saveData['mac'] = $arr[0];
            $saveData['add_time'] = date('Y-m-d H:i:s');
            $saveData['collect_time'] = $arr[1];

            Db::table('z_bm')->insert($saveData);
        }
        // return 'ok';
    }
    public function shengya($data)
    {
        $data1 = $data['result']['list'];
        $saveData = ['mac'=>'','maxTime'=>'','minTime'=>'','scan_count'=>'','add_time'=>''];
        foreach ($data1 as $key => $value) {
            $saveData['mac'] = $value['user_mac'];
            $saveData['maxTime'] = date('Y-m-d H:i:s',$value['maxTime']);
            // $saveData['minTime'] = date('Y-m-d H:i:s',$value['minTime']);
            $saveData['scan_count'] = $value['scan_count'];
            $saveData['add_time'] = date('Y-m-d H:i:s');
            Db::table('z_shengya')->insert($saveData);
        }
        return json(['success']);
    }

    public function overview()
    {
        
        // $this->assign([
        //     "datesky"=>$datesky,
        //     "bm"=>$bm,
        //     "tz006"=>$tz006,
        //     "shengya"=>$shengya
        //     ]);
        return $this->fetch();
    }

    public function getList()
    {
        $return['datesky'] = Db::table('z_datesky')->order('add_time desc')->field('mac,collect_time')->limit(5)->select();
        $return['bm'] = Db::table('z_bm')->order('add_time desc')->field('mac,collect_time')->limit(5)->select();
        $return['tz006'] = Db::table('z_tz006')->order('collect_time desc')->field('mac,collect_time')->limit(5)->select();
        $return['shengya'] = Db::table('z_shengya')->order('maxTime desc')->field('mac,maxTime')->limit(5)->select();
        $return['gprs'] = Db::table('mac_gprs_tcp_tb2')->order('add_time desc')->field('mac,collect_time')->limit(5)->select();
        return base_result($return, true);
    }

    public function count_mac_time($start_time = '' ,$end_time = '')
    {
        if (empty($start_time) || empty($end_time)) {
            return base_result(null, false, '缺少参数');
        }
        $start_time = str_replace('T', ' ', $start_time).':00';
        $end_time = str_replace('T', ' ', $end_time).':00';
        // dump($start_time);
        $datesky = Db::table('z_datesky')->where('collect_time','between',[$start_time,$end_time])->column('mac');
        $bm = Db::table('z_bm')->where('add_time','between time',[$start_time,$end_time])->column('mac');
        $tz006 = Db::table('z_tz006')->where('add_time','between time',[$start_time,$end_time])->column('mac');
        $shengya = Db::table('z_shengya')->where('maxTime','between time',[$start_time,$end_time])->column('mac');
        $gprs = Db::table('mac_gprs_tcp_tb2')->where('add_time','between time',[$start_time,$end_time])->column('mac');

        $return['count']['datesky']['count'] = count($datesky);
        $return['count']['bm']['count'] = count($bm);
        $return['count']['tz006']['count'] = count($tz006);
        $return['count']['shengya']['count'] = count($shengya);
        $return['count']['gprs']['count'] = count($gprs);

        $return['count']['datesky']['uicount'] = count(array_unique($datesky));
        $return['count']['bm']['uicount'] = count(array_unique($bm));
        $return['count']['tz006']['uicount'] = count(array_unique($tz006));
        $return['count']['shengya']['uicount'] = count(array_unique($shengya));
        $return['count']['gprs']['uicount'] = count(array_unique($gprs));

        $data['datesky'] = explode('--', strtolower(implode('--',$datesky)));
        $data['bm'] = explode('--', strtolower(implode('--',$bm)));
        $data['tz006'] = explode('--', strtolower(implode('--',$tz006)));
        $data['shengya'] = explode('--', strtolower(implode('--',$shengya)));
        $data['gprs'] = explode('--', strtolower(implode('--',$gprs)));
        $data_list = [];
        foreach ($data as $key => $value) {
            if (sizeof($value) > 1) {
                if (!empty($data_list)) {
                    $data_list = array_intersect($data_list, $value);
                } else {
                    $data_list = $value;
                }
            }
        }
        // $data_unique = array_unique($data_list);
        // $data_list = array_intersect($data['datesky'], $data['tz006'], $data['shengya'], $data['gprs']);
        $data_unique = array_unique($data_list);
        $return['count']['all_count'] = count($data_list);
        $return['count']['all_uicount'] = count($data_unique);

        return base_result($return, true);
    }

    public function mac_repeat()
    {
        set_time_limit(0);
        $start_time = '2017-11-17 16:45:00';
        $end_time = '2017-11-17 17:20:00';
        // $data = Db::table('z_datesky')->alias('d')->where('d.add_time','between time',[$start_time,$end_time])->join('z_bm b', 'd.mac = b.mac')->join('z_tz006 t', 't.mac = d.mac and t.mac = b.mac')->join('mac_gprs_tcp_tb2 m', 'm.mac = d.mac and m.mac = b.mac and m.mac = t.mac')->field('d.mac, d.add_time')->select();
        // if (Cache::get('datesky45')) {
        //     $datesky = Cache::get('datesky45');
        // } else {
        //     $datesky = Db::table('z_datesky')->where('collect_time','between',[$start_time,$end_time])->column('mac');
        //     Cache::set('datesky45', $datesky);
        // }
        // // dump(explode('--', strtolower(implode('--',array_unique($datesky)))));
        // if (Cache::get('tz00645')) {
        //     $tz006 = Cache::get('tz00645');
        // } else {
        //     $tz006 = Db::table('z_tz006')->where('add_time','between time',[$start_time,$end_time])->column('mac');
        //     Cache::set('tz00645', $tz006);
        // }
        // dump(explode('--', strtolower(implode('--',array_unique($tz006)))));
        // if (Cache::get('bm45')) {
        //     $bm = Cache::get('bm45');
        // } else {
        //     $bm = Db::table('z_bm')->where('add_time','between time',[$start_time,$end_time])->column('mac');
        //     Cache::set('bm45', $bm);
        // }

        // if (Cache::get('shengya45')) {
        //     $shengya = Cache::get('shengya45');
        // } else {
        //     $shengya = Db::table('z_shengya')->where('maxTime','between time',[$start_time,$end_time])->column('mac');
        //     Cache::set('shengya45', $shengya);
        // }

        // if (Cache::get('gprs45')) {
        //     $gprs = Cache::get('gprs45');
        // } else {
        //     $gprs = Db::table('mac_gprs_tcp_tb2')->where('add_time','between time',[$start_time,$end_time])->column('mac');
        //     Cache::set('gprs45', $gprs);
        // }

        $datesky = Db::table('z_datesky')->where('collect_time','between',[$start_time,$end_time])->column('mac');
        $bm = Db::table('z_bm')->where('add_time','between time',[$start_time,$end_time])->column('mac');
        $tz006 = Db::table('z_tz006')->where('add_time','between time',[$start_time,$end_time])->column('mac');
        $shengya = Db::table('z_shengya')->where('maxTime','between time',[$start_time,$end_time])->column('mac');
        $gprs = Db::table('mac_gprs_tcp_tb2')->where('add_time','between time',[$start_time,$end_time])->column('mac');

        $datesky = explode('--', strtolower(implode('--',$datesky)));
        // $bm = explode('--', strtolower(implode('--',$bm)));
        $tz006 = explode('--', strtolower(implode('--',$tz006)));
        $shengya = explode('--', strtolower(implode('--',$shengya)));
        $gprs = explode('--', strtolower(implode('--',$gprs)));

        // dump($datesky);
        // dump($gprs);
        // dump($tz006);
        // dump($shengya);

        $data = array_intersect($datesky, $tz006, $gprs, $shengya);
        $data1 = array_unique($data);
        dump($data);
        dump($data1);
    }

    public function findMac()
    {
        // $mac = '20:AB:37:91:50:59';
        $mac = 'C8:1E:E7:CA:B2:B4';
        $start_time = '2017-11-17 15:00:00';
        $end_time = '2017-11-17 15:20:00';

        $data['datesky'] = Db::table('z_datesky')->where('mac', $mac)->where('collect_time','between time',[$start_time,$end_time])->find();
        $data['bm'] = Db::table('z_bm')->where('mac', $mac)->where('add_time','between time',[$start_time,$end_time])->find();
        $data['tz006'] = Db::table('z_tz006')->where('mac', $mac)->where('add_time','between time',[$start_time,$end_time])->find();
        $data['shengya'] = Db::table('z_shengya')->where('mac', $mac)->where('maxTime','between time',[$start_time,$end_time])->find();
        $data['gprs'] = Db::table('mac_gprs_tcp_tb2')->where('mac', $mac)->where('add_time','between time',[$start_time,$end_time])->find();

        dump($data);
    }



}
